package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import data.dao.LeaveDAOException;
import data.dto.LeaveTypeDTO;

import manager.LeaveTypeManager;

/**
 * Servlet implementation class LoadData
 */
@WebServlet("/LoadData")
public class LoadData extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadData() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) {
		LeaveTypeManager ltm=new LeaveTypeManager();
		try {
			if(request.getParameter("type").equalsIgnoreCase("leavetype"))
			{
				ArrayList<LeaveTypeDTO> data=ltm.findAllLeaveType();
				request.setAttribute("leaveType", data);
				
				RequestDispatcher rd=request.getRequestDispatcher("/admin/ViewPage.jsp?type=leavetype");
				try {
					rd.forward(request, response);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}catch(IOException e){
					e.printStackTrace();
				}
			}
			else if(request.getParameter("type").equalsIgnoreCase("staff")){
				
				RequestDispatcher rd=request.getRequestDispatcher("/admin/ViewPage.jsp?type=staff");
				try {
					rd.forward(request, response);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}catch(IOException e){
					e.printStackTrace();
				}
				
			}
			
			
			//HttpSession session = request.getSession(true);
			//session.setAttribute("LeaveType", true) ;
			
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (data.dao.daoImplementation.SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
